<template>
	<view class="uni-container">
		<uni-forms ref="form" :model="formData" validateTrigger="bind" labelPosition="top" labelWidth="100">
			<uni-forms-item name="totalClassify_id" label="装备总类型" required>
				<uni-data-select v-model="formData.totalClassify_id" :localdata="totalClassifyRange"
					placeholder="请选择装备总类型"></uni-data-select>
			</uni-forms-item>
			<uni-forms-item name="classifyName" label="装备分类名称" required>
				<uni-easyinput placeholder="请输入装备分类名称" v-model="formData.classifyName" trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="classifyNickName" label="装备分类简称" required>
				<uni-easyinput placeholder="请输入装备分类简称" v-model="formData.classifyNickName" trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="directoryCategories" label="目录类别" required>
				<uni-data-select v-model="formData.directoryCategories" :localdata="directoryCategoryList"
					placeholder="请选择目录类别"></uni-data-select>
			</uni-forms-item>
			<uni-forms-item name="directoryIndex" label="指导目录号" required>
				<uni-easyinput placeholder="设备归类编号(具体需要以基层防灾项目指导目录序号填充)" v-model="formData.directoryIndex"
					trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="state" label="启用状态">
				<switch @change="binddata('state', $event.detail.value)" :checked="formData.state"></switch>
			</uni-forms-item>
			<view class="uni-button-group">
				<button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
				<navigator open-type="navigateBack" style="margin-left: 15px;">
					<button class="uni-button" style="width: 100px;">返回</button>
				</navigator>
			</view>
		</uni-forms>
	</view>
</template>

<script>
	import {
		validator
	} from '@/js_sdk/validator/tab_EquipmentClassify.js';

	const db = uniCloud.database();
	const dbCmd = db.command;
	const dbCollectionName = 'tab_EquipmentClassify';

	function getValidator(fields) {
		let result = {}
		for (let key in validator) {
			if (fields.includes(key)) {
				result[key] = validator[key]
			}
		}
		return result
	}



	export default {
		data() {
			let formData = {
				"totalClassify_id": "",
				"classifyName": "",
				"classifyNickName": "",
				"directoryCategories": "",
				"directoryIndex": "",
				"state": true
			}
			return {
				formData,
				formOptions: {},
				rules: {
					...getValidator(Object.keys(formData))
				},
				directoryCategoryList: [{
						value: "市级",
						text: "市级"
					},
					{
						value: "县级",
						text: "县级"
					},
					{
						value: "乡级",
						text: "乡级"
					},
				],
				totalClassifyRange: []
			}
		},
		onLoad() {
			this.getTotalClassify()
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {

			/**
			 * 验证表单并提交
			 */
			submit() {
				uni.showLoading({
					mask: true
				})
				this.$refs.form.validate().then((res) => {
					return this.submitForm(res)
				}).catch(() => {}).finally(() => {
					uni.hideLoading()
				})
			},

			/**
			 * 提交表单
			 */
			submitForm(value) {
				// 使用 clientDB 提交数据
				return db.collection(dbCollectionName).add(value).then((res) => {
					uni.showToast({
						title: '新增成功'
					})
					this.getOpenerEventChannel().emit('refreshData')
					setTimeout(() => uni.navigateBack(), 500)
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				})
			},
			async getTotalClassify() {
				await db.collection('tab_EquipmentTotalClassify').field("totalKeyName,totalKeyValue").get().then((
					res) => {
						// console.log(res.result.data)
						const receivedData = res.result.data
						this.totalClassifyRange = receivedData.map(item => ({
							value: item._id,
							text: `${item.totalKeyValue}_${item.totalKeyName}`
						}));
						//默认显示第一个
						this.formData.totalClassify_id = this.totalClassifyRange[0].value
					}).catch((err) => {
					uni.showModal({
						content: err.message || '获取装备总类别失败',
						showCancel: false
					})
				})
			}
		}
	}
</script>